var express = require("express");
var router = express.Router();
var db = require("../db/index");

router.post('/add', function (req, res, next) {
    let {title, content, user_id, society_id, img, creationTime} = req.body;
    db.query(`INSERT INTO talk set ?`, {
        title, content, user_id, society_id, img, creationTime
    }, function (err) {
        console.log(err)
        if (!err) {
            res.send({
                code: 200,
                message: '成功'
            })
        } else {
            res.send({
                code: 20009,
                message: err.message
            })
        }
    })
})

router.get('/get/all', function (req, res) {
    let {society_id, pageNo, limit, orderBy} = req.query;
    let start = (pageNo - 1) * limit
    db.query(`SELECT t.*,a.name as createrName FROM talk t LEFT JOIN account a ON t.user_id = a.id WHERE t.society_id = ${society_id} ORDER BY ${orderBy} LIMIT ${start},${limit}`,
        function (err, rows) {
            if (!err) {
                db.query(`SELECT count(id) AS total FROM talk WHERE society_id = ${society_id}`, async function (err2, countArr) {
                    let talks = await Promise.all(
                        rows.map(item => {
                            return new Promise(resolve => {
                                db.query(`SELECT count(id) as likes FROM talk_like WHERE talk_id = ${item.id}`, function (err3, row2) {
                                    item.likes = row2[0].likes;
                                    resolve(item);
                                })
                            })
                        })
                    )
                    res.send({
                        code: 200,
                        message: '成功',
                        talks,
                        total: countArr[0].total
                    })
                })
            } else {
                res.send({
                    code: 20012,
                    message: '失败'
                })
            }
        })
})

router.get('/like', function (req, res) {
    let {talk_id, user_id} = req.query;
    let sql = `INSERT INTO talk_like set ?`;
    db.query(sql, {talk_id, user_id}, function (err) {
        if (!err) {
            res.send({
                code: 200,
                message: '成功'
            })
        } else {
            res.send({
                code: 20029,
                message: err.message
            })
        }
    })
})

router.get('/view', function (req, res) {
    let {id} = req.query;
    let sql = `UPDATE talk set views = views + 1 WHERE id = ${id}`;
    db.query(sql, function (err) {
        if (!err) {
            res.send({
                code: 200,
                message: '成功'
            })
        } else {
            res.send({
                code: 20029,
                message: err.message
            })
        }
    })
})

router.get('/del', function (req, res) {
    let {id} = req.query;
    let sql = `DELETE FROM talk WHERE id = ${id}`;
    db.query(sql, function (err) {
        if (!err) {
            res.send({
                code: 200,
                message: '成功'
            })
        } else {
            res.send({
                code: 20029,
                message: err.message
            })
        }
    })
})
module.exports = router;
